package com.liangzhong.proxy.winebar
{
	import com.adobe.serialization.json.JSON;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.commandconst.winebar.CMD_Winebar;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.winebar.WinebarServerDataParse;
	import com.liangzhong.model.winebar.vo.WineBarInfoVo;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.winebar.WinebarConst;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *酒吧 
	 * @author lake
	 * 
	 */	
	public class WinebarProxy extends BaseProxy
	{
		public static const ProxyName :String = "WinebarProxy";
		
		public function WinebarProxy(proxyName:String=ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Logic.MAIN:
				{
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
				}
			}
			return false;
		}
		/**
		 *处理信息 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			var resObj :Object;
			var retcode :int;
			switch(subCmdID)
			{
				case CMD_Winebar.SUB_GRADE_UP://升级
				{
					resObj = WinebarServerDataParse.readGradeUp(buffer);
					this.sendNotification(WinebarConst.GRADE_UP_RESULT,resObj);
					return true;
				}
				case CMD_Winebar.SUB_ENTER_BAR://进入酒吧 
				{
					retcode = buffer.readInt();
					if(retcode != 0)
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					return true;
				}
				case CMD_Winebar.SUB_LEAVE_BAR://离开酒吧 返回结算 
				{
					retcode = buffer.readInt();
					resObj = {};
					resObj.retcode = retcode;
					resObj.type = buffer.readByte();
					resObj.exp = buffer.readInt();
					if(retcode != 0)
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					else
					{
						this.sendNotification(WinebarConst.LEAVE_RESULT,resObj);
					}
					return true;
				}
				case CMD_Winebar.SUB_RECEIVE_AWARD://领取奖励 
				{
					retcode = buffer.readInt();
					resObj = {};
					resObj.retcode = retcode;
					if(retcode != 0)
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					else
					{
					}
					this.sendNotification(WinebarConst.BUY_RESULT,resObj);
					return true;
				}
			}
			return false;
		}
		/**
		 * 升级
		 */		
		public function gradeUp():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,CMD_Winebar.SUB_GRADE_UP,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *进入酒吧 
		 * 
		 */		
		public function enterBar():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,CMD_Winebar.SUB_ENTER_BAR,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *离开酒吧 返回结算 
		 */		
		public function leaveBar():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,CMD_Winebar.SUB_LEAVE_BAR,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
			//test
			//this.sendNotification(WinebarConst.LEAVE_RESULT,{type:3,exp:8888000});
		}
		/**
		 *领取奖励 
		 * @param type
		 *  1-	100W欧元
			2-	10W经验
			3-	10W声望
			4-	100W欧元、10W经验、10W声望
			5-	额外经验
		 */		
		public function gewAward(type :int):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,CMD_Logic.MAIN,CMD_Winebar.SUB_RECEIVE_AWARD,0);
			sendBuf.writeByte(type);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
			//test
			//this.sendNotification(WinebarConst.BUY_RESULT);
		}
		/**
		 *请求酒吧信息 
		 * @param $obj
		 * 
		 */
		public function getWinebarInfo(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.WINEBAR_GET_INFO;
			ProxyUtils.instance.webClient(getWinebarInfoRes,variables,false);
		}
		/**
		 *返回信息 
		 * @param $data
		 * 
		 */
		private function getWinebarInfoRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().winebarManager.parseBaseInfo(obj.bar);
				var wineBarInfo :WineBarInfoVo = AppData.getInstance().winebarManager.wineBarInfoVo;
				//判断是否有离线泡吧经验
				if(AppData.getInstance().user.isWineAwardFirst && 
					wineBarInfo.begintime > 0)
				{
					if(wineBarInfo.ntype > 0)
					{
						var curTime :int = AppData.getInstance().time;
						var grapTime :int = curTime - wineBarInfo.begintime;
						var expTime	:int = AppData.getInstance().winebarManager.expTime * 3600;
						var levelExp :int = AppData.getInstance().winebarManager.levelExp;
						var exp :int = (grapTime<expTime)?(levelExp*grapTime):(levelExp * expTime);
						var resObj :Object = {};
						resObj.retcode = 0;
						resObj.type = wineBarInfo.ntype;
						resObj.exp = exp;
						this.sendNotification(WinebarConst.LEAVE_RESULT,resObj);
					}
					else
					{
						this.sendNotification(ApplicationConstants.WINEBAR_LEAVE);
					}
				}
				AppData.getInstance().user.isWineAwardFirst = false;
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
	}
}